package com.cqq.auth.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cqq.auth.constant.AuthMessage;
import com.cqq.auth.dao.UserGroupDao;
import com.cqq.auth.dao.domain.UserGroup;
import com.cqq.auth.dao.query.UserGroupQuery;
import com.cqq.auth.service.UserGroupService;
import com.cqq.auth.service.bean.UserGroupBean;
import com.cqq.auth.service.bean.UserGroupQueryBean;
import com.cqq.auth.service.exception.UserGroupServiceException;
import com.cqq.common.core.base.BaseService;
import com.cqq.common.core.bean.PageInfoBean;
import com.cqq.common.core.constant.CommonMessage;
import com.cqq.common.core.util.BeanUtil;

@Service("userGroupService")
public class UserGroupServiceImpl extends BaseService implements UserGroupService {

	private static final Logger log = LoggerFactory.getLogger(UserGroupServiceImpl.class);
	
	@Autowired
	private UserGroupDao userGroupDao;
	
	
	public void addUserGroup(UserGroupBean group)
			throws UserGroupServiceException {
		log.debug("addUserGroup ...");
		if(group==null || StringUtils.isBlank(group.getName())){
			throw super.buildException(new UserGroupServiceException(CommonMessage.COMMON_PARAM_EMPTY));
		}
		
		UserGroup g=this.userGroupDao.queryByName(group.getName());
		if(g!=null){
			throw super.buildException(new UserGroupServiceException(AuthMessage.GROUP_NAME_DUPLICATE));
		}
		
		g=new UserGroup();
		BeanUtil.copy(group, g);
		this.userGroupDao.add(g);
		group.setId(g.getId());
		log.debug("addUserGroup ok! {}",group);
	}

	
	public void updateUserGroup(UserGroupBean group)
			throws UserGroupServiceException {
		log.debug("updateUserGroup ...");
		if(group==null || group.getId()==null
				|| StringUtils.isBlank(group.getName())){
			throw super.buildException(new UserGroupServiceException(CommonMessage.COMMON_PARAM_EMPTY));
		}
		
		UserGroup g=this.userGroupDao.queryByName(group.getName());
		if(g!=null && !g.getId().equals(group.getId())){
			throw super.buildException(new UserGroupServiceException(AuthMessage.GROUP_NAME_DUPLICATE));
		}
		
		g=new UserGroup();
		BeanUtil.copy(group, g);
		if(this.userGroupDao.update(g)!=1){
			throw super.buildException(new UserGroupServiceException(CommonMessage.COMMON_UPDATE_ERROR));
		}
		log.debug("updateUserGroup ok! {}",group);
	}

	
	public void deleteUserGroup(Integer id) throws UserGroupServiceException {
		log.debug("deleteUserGroup ...");
		UserGroup g=this.userGroupDao.queryById(id);
		if(g==null){
			throw super.buildException(new UserGroupServiceException(AuthMessage.GROUP_NOT_EXIST));
		}
		if(this.userGroupDao.delete(id)!=1){
			throw super.buildException(new UserGroupServiceException(CommonMessage.COMMON_DELETE_ERROR));
		}
		log.debug("deleteUserGroup ok! id:{}",id);
	}

	
	public UserGroupBean findUserGroup(Integer id) {
		if(id==null){
			return null;
		}
		UserGroup group=this.userGroupDao.queryById(id);
		UserGroupBean bean=new UserGroupBean();
		BeanUtil.copy(group, bean);
		return bean;
	}

	
	public List<UserGroupBean> listAllUserGroup() {
		List<UserGroup> groups=this.userGroupDao.queryAll();
		List<UserGroupBean> beans=BeanUtil.copyList(groups, UserGroupBean.class);
		return beans;
	}

	
	public PageInfoBean<UserGroupBean> queryUserGroupByPage(
			UserGroupQueryBean queryBean) {
		// TODO Auto-generated method stub
		log.debug("queryUserGroupByPage ...");
		if(queryBean==null){
			return null;
		}
		UserGroupQuery query=new UserGroupQuery();
		BeanUtil.copy(queryBean, query);
		List<UserGroupBean> beanList=null;
		Integer count=userGroupDao.queryCount(query);
		if(count>0){
			List<UserGroup> users=this.userGroupDao.queryPage(query);
			beanList=BeanUtil.copyList(users, UserGroupBean.class);
		}
		PageInfoBean<UserGroupBean> page=new PageInfoBean<UserGroupBean>(beanList,count,queryBean.getPageSize(),queryBean.getPageNo());
		log.debug("queryUsersByPage ok! {}",queryBean);
		return page;
	}

	
	public void updateUserGroupRoles(Integer userGroupId, Integer[] roleIds)
			throws UserGroupServiceException {
		log.debug("updateUserGroupRoles ...");
		if(userGroupId==null || roleIds==null || roleIds.length==0){
			throw super.buildException(new UserGroupServiceException(CommonMessage.COMMON_PARAM_EMPTY));
		}
		UserGroup user=this.userGroupDao.queryById(userGroupId);
		if(user==null){
			throw super.buildException(new UserGroupServiceException(AuthMessage.USER_NOT_EXIST));
		}
		this.userGroupDao.updateRoleIds(userGroupId, roleIds);
		log.debug("updateUserGroupRoles ok! userId={},roleIds={}", userGroupId, roleIds);
		
	}

	
	public List<UserGroupBean> listUserGroupByUserId(Integer userId) {
		// TODO Auto-generated method stub
		List<UserGroup> groups=this.userGroupDao.queryUserGroupByUserId(userId);
		List<UserGroupBean> beans=BeanUtil.copyList(groups, UserGroupBean.class);
		return beans;
	}

}
